#define _CRT_SECURE_NO_WARNINGS
#include"test.h"

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
    {
        unordered_map<int, vector<int>> edges;
        vector<int> in(numCourses, 0);
        vector<int> ret;
        for (auto& e : prerequisites)
        {
            int dst = e[0], src = e[1];
            edges[src].push_back(dst);
            ++in[dst];
        }

        queue<int> q;
        for (int i = 0; i < numCourses; i++)
        {
            if (in[i] == 0)
                q.push(i);
        }

        while (!q.empty())
        {
            auto front = q.front();
            ret.push_back(front);
            q.pop();
            for (auto point : edges[front])
            {
                if (--in[point] == 0) q.push(point);
            }
        }
        for (int i = 0; i < numCourses; i++)
        {
            if (in[i] != 0) return {};
        }
        return ret;
    }
};